
package shopping

import (
    "time"
	//"errors"
	//"strings"
	"dhfshop/models"
	"dhfshop/models/consts"
	"github.com/astaxie/beego/orm"
)

type DeliverInfo struct {
    Id           int64          `orm:"column(id);pk;auto"`
	ExpressTrace *ExpressTrace  `orm:"column(express_trace_id);rel(fk);on_delete(cascade)" json:"-" ` 
	StatusTime   string         `orm:"column(status_time);size(128);null" json:"status_time"`           
	StatusDesc   string         `orm:"column(status_desc);size(128);null" json:"status_desc"` 
}

/* 订单物流信息 */
type ExpressTrace struct {
    ExpressTraceId  int64           `orm:"column(express_trace_id);pk;auto"`               
	MemberId        int64           `orm:"column(member_id);index" json:"member_id" form:"member_id"`
	PartnerId       int64             `orm:"column(partner_id)" json:"partner_id" form:"partner_id"`       //
	CreatedAt       time.Time       `orm:"auto_now_add;column(created_at);type(datetime)" json:"-"`
    CreatedAtStr    string          `orm:"-" json:"created_at"`
    UpdatedAt       time.Time       `orm:"column(updated_at);type(datetime)" json:"-"`	
    UpdatedAtStr    string          `orm:"-" json:"updated_at"`
    ExpressName     string          `orm:"column(express_name);size(128);null" json:"express_name"`   //物流公司名称
    ShippingCode    string          `orm:"column(shipping_code);size(128);index" json:"shipping_code"` //物流编号  	
	Status          string          `orm:"column(status);size(32);null" json:"status"`           
	DeliverInfo     []*DeliverInfo  `orm:"reverse(many)" json:"deliver_info"`  //物流信息  	
}

func init() {
    orm.RegisterModel(new(ExpressTrace),new(DeliverInfo))   
}

// 创建地址
func AddExpressTrace(v *ExpressTrace) (id int64, err error) {
	o := models.GetDefaultDB()
	id, err = o.Insert(v)
	return
}
// 更新地址
func UpdateExpressTrace(v *ExpressTrace) (id int64, err error) {
	o := models.GetDefaultDB()
	m := ExpressTrace{ExpressTraceId: v.ExpressTraceId}
	if err = o.Read(&m); err == nil {
		if id, err = o.Update(v); err == nil {
		    return
		}
	}
	return
}

func GetExpressTraceById(id int64) (v *ExpressTrace, err error) {
	o := models.GetDefaultDB()
	v = &ExpressTrace{ExpressTraceId: id}
	if err = o.Read(v); err == nil {
	    v.CreatedAtStr = v.CreatedAt.Format(consts.DATETIME_FORMAT)
		v.UpdatedAtStr = v.UpdatedAt.Format(consts.DATETIME_FORMAT)
	    o.LoadRelated(v, "DeliverInfo")
		for _, info := range v.DeliverInfo {
			info.ExpressTrace = nil
		}
		return v, nil
	}
	return nil, err
}

func GetExpressTraceByShipCode(code string) (v *ExpressTrace, err error) {
	o := models.GetDefaultDB()
	v = &ExpressTrace{ShippingCode: code}    //多个的话,read中需要指明列
	if err = o.Read(v,"ShippingCode"); err == nil {
	    v.CreatedAtStr = v.CreatedAt.Format(consts.DATETIME_FORMAT)
		v.UpdatedAtStr = v.UpdatedAt.Format(consts.DATETIME_FORMAT)
	    o.LoadRelated(v, "DeliverInfo")
		for _, info := range v.DeliverInfo {
			info.ExpressTrace = nil
		}
		return v, nil
	}
	return nil, err
}

func GetAllExpressTrace(memberId int)(lists []*ExpressTrace, err error){
	o := models.GetDefaultDB()
	_, err = o.QueryTable(new(ExpressTrace)).Filter("member_id", memberId).OrderBy("-address_id").All(&lists)
	return
}

func DeleteExpressTrace(id int64) (err error) {
	o := models.GetDefaultDB()
	v := ExpressTrace{ExpressTraceId: id}
	if err = o.Read(&v); err == nil {
		if _, err = o.Delete(&v); err == nil {
			return
		}
	}
	return
}
